﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;

namespace VehicleMaintenance.BaseInfo
{
    public partial class InventoryForm : ListBaseForm
    {
        public InventoryForm()
        {
            InitializeComponent();
        }

        private Goods good;

        public Goods Good
        {
            get { return good; }
            set { good = value; }
        }

        private void CarListForm_Load(object sender, EventArgs e)
        {
            try
            {
                btnAdd.Visible = false;
                btnDelete.Visible = false;
                btnEdit.Text = "调整库存";
                BindData();
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    GoodsForm form = new GoodsForm();
            //    form.ShowDialog(this);

            //    BindData();
            //}
            //catch (Exception ex)
            //{
            //    this.ShowInfo(ex.Message);
            //}
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                if (lvData.SelectedItems.Count > 0)
                {
                    //GoodsForm form = new GoodsForm(Convert.ToInt32(lvData.SelectedItems[0].Tag));
                    //form.ShowDialog(this);

                    //BindData();
                    ReviseInventoryForm form = new ReviseInventoryForm(Convert.ToInt32(lvData.SelectedItems[0].Tag));
                    form.ShowInTaskbar = false;
                    if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                    {
                        BindData();
                    }
                }
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    if (lvData.SelectedItems.Count > 0)
            //    {
            //        if (MessageBox.Show(this, "是否删除当前车辆信息？", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
            //        {
            //            CarDBContext db = new CarDBContext();

            //            int id = Convert.ToInt32(lvData.SelectedItems[0].Tag);
            //            db.Goods.Remove(db.Goods.First(p => p.GoodID == id));

            //            db.SaveChanges();

            //            BindData();
            //        }
            //    }
            //}
            //catch (Exception ex)
            //{
            //    this.ShowInfo(ex.Message);
            //}
        }

        private void BindData()
        {
            try
            {
                lvData.Items.Clear();

                CarDBContext db = new CarDBContext();
                var list = db.Goods.Include("CarType").Include("GoodType").OrderBy(p => p.GoodID);

                foreach (var item in list)
                {
                    ListViewItem lvItem = new ListViewItem(new string[] { item.GoodType.Name,item.Code,  item.Name,item.CarType.Name, item.Placeoforigin, 
                                item.Units,item.Number.ToString(),item.Top.ToString(),item.Bottom.ToString(),item.Remark});
                    lvItem.Tag = item.GoodID;
                    lvData.Items.Add(lvItem);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void lvData_DoubleClick(object sender, EventArgs e)
        {
            //if (lvData.SelectedItems.Count > 0)
            //{
            //    CarDBContext db = new CarDBContext();

            //    int id = Convert.ToInt32(lvData.SelectedItems[0].Tag);
            //    good = db.Goods.First(p => p.GoodID == id);

            //    this.DialogResult = System.Windows.Forms.DialogResult.OK;
            //}
        }

    }
}
